/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_sip_analyze_header.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/16 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/sip/config.h"
#include <sip/sip_core.h>
#include <sip/sip_header.h>
#include <sip/sip_comm.h>
#include <sip/sip_packet.h>

extern void __sip_destroy_packet(L7_sip_packet **pp_st_sip_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正确的请求头
 * INVITE
 */
static void __case1(void)
{
    char *p_payload = (char *) "INVITE sip:42010606001320082255_1@10.72.1.125 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "a=recvonly\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "INVITE");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_INVITE);
    CU_ASSERT_EQUAL(i_content_len, 276);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * ACK
 */
static void __case2(void)
{
    char *p_payload = (char *) "ACK sip:42010606001320082255_1@10.72.1.125:16068 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.1.125:5060;rport;branch=z9hG4bK1366911481\r\n"
            "From: <sip:42000000004002685574@10.72.1.125>;tag=1725246541\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125:16068>;tag=1653620743\r\n"
            "Call-ID: 1834731653@10.72.1.125\r\n"
            "CSeq: 20 ACK\r\n"
            "Contact: <sip:42000000004002685574@10.72.1.125:5060>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Content-Length: 0\r\n"
            "\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "ACK");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_ACK);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * BYE
 */
static void __case3(void)
{
    char *p_payload = (char *) "BYE sip:42010606001320082255@10.73.0.214:8086 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.1.125:16068;rport;branch=z9hG4bK1492398355\r\n"
            "From: <sip:42000000002007981673@10.72.1.125>;tag=86779546\r\n"
            "To: <sip:42010606001320082255@10.73.0.214:8086>;tag=227095533\r\n"
            "Call-ID: 72653873@10.72.1.125\r\n"
            "CSeq: 21 BYE\r\n"
            "Contact: <sip:42000000002007981673@10.72.1.125:16068>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Content-Length: 0\r\n"
            "\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "BYE");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_BYE);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * CANCEL
 */
static void __case4(void)
{
    char *p_payload = (char *) "CANCEL sip:+86-18611033311@ims.cn;user=phone SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.242.134.215:5063;branch=z9hG4bK1iv2zrar3ap3havxrvil8hzal\r\n"
            "Route: <sip:10.242.134.196;lr;ORGDLGID=7184-5;Dpt=7b84_6;TRC=5b5d-ffffffff>\r\n"
            "Call-ID: jppz0jhz802x01rv1jz3vizz1vgzjp2j@ATS.ats01.ims.cn.131\r\n"
            "From: <sip:+867916184196@ms.cn>;tag=z8p0aj28-CC-131-TRC-102964\r\n"
            "To: <sip:18611033311@ims.cn;user=phone>\r\n"
            "CSeq: 1 CANCEL\r\n"
            "Max-Forwards: 70\r\n"
            "Content-Length: 0\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "CANCEL");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_CANCEL);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * OPTIONS
 */
static void __case5(void)
{
    char *p_payload = (char *) "OPTIONS sip:carol@chicago.com SIP/2.0\r\n"
            "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKhjhs8ass877\r\n"
            "Max-Forwards: 70\r\n"
            "To: <sip:carol@chicago.com>\r\n"
            "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n"
            "Call-ID: a84b4c76e66710\r\n"
            "CSeq: 63104 OPTIONS\r\n"
            "Contact: <sip:alice@pc33.atlanta.com>\r\n"
            "Accept: application/sdp\r\n"
            "Content-Length: 0\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "OPTIONS");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_OPTIONS);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * REGISTER
 */
static void __case6(void)
{
    char *p_payload = (char *) "REGISTER sip:42000000002008411353@10.72.1.125:5060 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.1.125:6068;rport;branch=z9hG4bK1258929990\r\n"
            "From: <sip:42000000002005936201@10.72.1.125:6068>;tag=733479827\r\n"
            "To: <sip:42000000002005936201@10.72.1.125:6068>\r\n"
            "Call-ID: 2045448992@10.72.1.125\r\n"
            "CSeq: 2778 REGISTER\r\n"
            "Contact: <sip:42000000002005936201@10.72.1.125:6068>\r\n"
            "Authorization: Digest username=\"IDU\", realm=\"420000\", nonce=\"725930730576431442350429890\", uri=\"sip:42000000002008411353@10.72.1.125:5060\", response=\"81d9d6c463ee85fdc1cfe682b5bc2db5\", algorithm=MD5, cnonce=\"0a4f113b\", opaque=\"725930730576431442350429890\", qop=auth, nc=00000001\r\n"
            "Max-Forwards: 5\r\n"
            "Expires: 180\r\n"
            "Content-Length: 0\r\n"
            "\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "REGISTER");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_REGISTER);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * PRACK
 */
static void __case7(void)
{
    char *p_payload = (char *) "PRACK sip:191.138.100.10:5060 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 164.129.32.80:10009;branch=z9hG4bKc84c575e589672d2befc0f1a91bbecea\r\n"
            "Route: <sip:191.138.100.5;lr;Dpt=7882_136;Role=3;CxtId=3;spln=PS;X-HwB2bUaCookie=16024;TRC=34c5-ffffffff>\r\n"
            "Call-ID: sbcb070c05c72f4c786fbd9db1f36b0ce78\r\n"
            "From: <sip:+8675552371010@s15.huawei.com;user=phone>;tag=b070c05c72f4c786fbd9db1f36b0ce78\r\n"
            "To: <sip:+8675552371020@s15.huawei.com;user=phone>;tag=qo1uyuww-CC-22-TRC-5650\r\n"
            "CSeq: 27971 PRACK\r\n"
            "Max-Forwards: 70\r\n"
            "RAck: 1 27970 INVITE\r\n"
            "Content-Length: 0\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "PRACK");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_PRACK);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * SUBSCRIBE
 */
static void __case8(void)
{
    char *p_payload = (char *) "SUBSCRIBE sip:+8675520000001@c8.huawei.com SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 164.132.176.2:5060;branch=z9hG4bKRs-3_r-3*T5etjODtjSUUo2dNl;rport\r\n"
            "To: \"+8675520000001\"<sip:+8675520000001@c8.huawei.com>\r\n"
            "From: \"+8675520000001\"<sip:+8675520000001@c8.huawei.com>;tag=diINxlINw7Tq-t0j-t4-U7RmD8\r\n"
            "Contact: sip:+8675520000001@164.132.176.2:5060\r\n"
            "Supported: 100rel,join,norefersub,path,replaces,timer\r\n"
            "Event: reg\r\n"
            "Accept: application/reginfo+xml\r\n"
            "Expires: 3660\r\n"
            "Call-ID: Ss-38r-39T5esjODsjSUUo2dNl@164.132.176.2\r\n"
            "CSeq: 1 SUBSCRIBE\r\n"
            "Max-Forwards: 70\r\n"
            "\r\n"
            "User-Agent: MSF/1.0.1518\r\n"
            "Content-Length: 0\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "SUBSCRIBE");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_SUBSCRIBE);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * NOTIFY
 */
static void __case9(void)
{
    char *p_payload = (char *) "NOTIFY sip:+8675520000001@164.131.128.107:10993 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 154.133.128.7:5060;branch=z9hG4bKzytkv0kyeer0text0zw01tv1k;Role=3;Dpt=7b82_36;X-HwDim=3;TRC=5b27-ffffffff\r\n"
            "Call-ID: asbc*hLl_3LljE+5I-GSI-KFhkRTon@164.192.96.100\r\n"
            "From: \"+8675520000001\"<sip:+8675520000001@c8.huawei.com>;tag=3edw3tzl\r\n"
            "To: \"+8675520000001\"<sip:+8675520000001@c8.huawei.com>;tag=HU5UJA5Uw7Tq-t0j-t4-T.Ai*4\r\n"
            "CSeq: 1 NOTIFY\r\n"
            "Contact: <sip:scscf.c8.huawei.com:5060;Dpt=7b84_16;RegSubIdx=533fa769;trans>\r\n"
            "Event: reg\r\n"
            "Subscription-State: active;expires=900\r\n"
            "Max-Forwards: 70\r\n"
            "Content-Length: 574 \r\n"
            "Content-Type: application/reginfo+xml\r\n"
            "\r\n"
            "<?xml version=\"1.0\"?> \r\n"
            "<reginfo xmlns=\"urn:ietf:params:xml:ns:reginfo\" version=\"0\" state=\"full\">\r\n"
            "<registration aor=\"sip:+8675520000001@c8.huawei.com\" id=\"28939bd\" state=\"active\">\r\n"
            "<contact id=\"8258470\" state=\"active\" event=\"registered\" >\r\n"
            "<uri>sip:+8675520000001@164.131.128.107:10993;transport=udp</uri> </contact>\r\n"
            "</registration>\r\n"
            "<registration aor=\"tel:+8675520000001\" id=\"f024781\" state=\"active\"> <contact id=\"8258470\" state=\"active\" event=\"created\" >\r\n"
            "<uri>sip:+8675520000001@164.131.128.107:10993;transport=udp</uri> </contact>\r\n"
            "</registration>\r\n"
            "</reginfo>\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "NOTIFY");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_NOTIFY);
    CU_ASSERT_EQUAL(i_content_len, 574);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * PUBLISH
 */
static void __case10(void)
{
    char *p_payload = (char *) "PUBLISH sip:13651229542@bjmcc.com SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 192.168.11.2:5060;rport;branch=z9hG4bK3074320496\r\n"
            "From: 13651229542<sip:13651229542@bjmcc.com>;tag=936503320\r\n"
            "To: <sip:13651229542@bjmcc.com>\r\n"
            "Call-ID: 217453280\r\n"
            "CSeq: 1 PUBLISH\r\n"
            "Contact: <sip:13651229542@192.168.11.2:5060>\r\n"
            "Content-Type: application/poc-settings+xml\r\n"
            "P-Preferred-Identity: 13651229542<sip:13651229542@bjmcc.com>\r\n"
            "Max-Forwards: 70\r\n"
            "User-Agent: XXX POC 2.0\r\n"
            "Expires: 3600\r\n"
            "Event: poc-settings\r\n"
            "Accept-Contact: *;+g.poc.talkburst;require;explicit\r\n"
            "Supported: 100rel,eventlist,timer,multiple-refer\r\n"
            "Content-Length:   602\r\n"
            "\r\n"
            "<?xml version=”1.0″ encoding=”UTF-8″?>\r\n"
            "<poc-settings xmlns=”urn:oma:params:xml:ns:poc:poc-settings” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”urn:oma:params:xml:ns:poc:poc-settings”>\r\n"
            "<entity id=”sip:13651229542@bjmcc.com”>\r\n"
            "<isb-settings>\r\n"
            "<incoming-session-barring active=”false” />\r\n"
            "</isb-settings>\r\n"
            "<am-settings>\r\n"
            "<answer-mode>automatic</answer-mode>\r\n"
            "</am-settings>\r\n"
            "<ipab-settings>\r\n"
            "<incoming-personal-alert-barring active=”false” />\r\n"
            "</ipab-settings>\r\n"
            "<sss-settings>\r\n"
            "<simultaneous-sessions-support active=”true” />\r\n"
            "</sss-settings>\r\n"
            "</entity>\r\n"
            "</poc-settings>\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "PUBLISH");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_PUBLISH);
    CU_ASSERT_EQUAL(i_content_len, 602);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * INFO
 */
static void __case11(void)
{
    char *p_payload = (char *) "INFO sip:191.133.7.70:5060;Dpt=757a_16 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 191.132.60.151:5061;branch=z9hG4bKxrxxxqsw1xwgx411f5rshwu4q\r\n"
            "Route: <sip:191.133.7.70:5060;lr;Dpt=757a_116;Role=3;CxtId=3;X-HwB2bUaCookie=15008>\r\n"
            "Call-ID: whqxhqow1ygvu44qo5ui44goifvh5uhr@ATS.ats151.domain60.huawei.com.18\r\n"
            "From: <sip:ats151.domain60.huawei.com@domain60.huawei.com>;tag=rgii1oyi-CC-18-TRC-3033\r\n"
            "To: <sip:msml@191.133.7.70:5060>;tag=2e7per24\r\n"
            "CSeq: 2 INFO\r\n"
            "Max-Forwards: 70\r\n"
            "Supported: timer\r\n"
            "Content-Length: 387\r\n"
            "Content-Type: application/msml+xml\r\n"
            "\r\n"
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
            "<msml version=\"1.1\">\r\n"
            "<dialogstart target=\"conn:2e7per24\" type=\"application/moml+xml\" name=\"HuaweiATSAsDialog18181308758635\">\r\n"
            "    <play barge=\"true\" cleardb=\"true\" iterate=\"-1\" interval=\"1\">\r\n"
            "        <audio uri=\"28\" xml:lang=\"eng\"/>\r\n"
            "        <playexit>\r\n"
            "        <exit namelist=\"play.end play.amt\"/>\r\n"
            "        </playexit>\r\n"
            "</play>\r\n"
            "</dialogstart>\r\n"
            "</msml>\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "INFO");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_INFO);
    CU_ASSERT_EQUAL(i_content_len, 387);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * REFER
 */
static void __case12(void)
{
    char *p_payload = (char *) "REFER sip:b@atlanta.example.com SIP/2.0\r\n"
            "Via: SIP/2.0/UDP agenta.atlanta.example.com;branch=z9hG4bK2293940223\r\n"
            "To: <sip:b@atlanta.example.com>\r\n"
            "From: <sip:a@atlanta.example.com>;tag=193402342\r\n"
            "Call-ID: 898234234@agenta.atlanta.example.com\r\n"
            "CSeq: 93809823 REFER\r\n"
            "Max-Forwards: 70\r\n"
            "Refer-To: c@atlanta.example.com\r\n"
            "Contact: sip:a@atlanta.example.com\r\n"
            "Content-Length: 0\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "REFER");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_REFER);
    CU_ASSERT_EQUAL(i_content_len, 0);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * MESSAGE
 */
static void __case13(void)
{
    char *p_payload = (char *) "MESSAGE sip:42000000002023352126@10.72.38.12:5061 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.1.125:5060;rport;branch=z9hG4bK468609758\r\n"
            "From: <sip:42000000002008411353@10.72.1.125:5060>;tag=351315153\r\n"
            "To: <sip:42000000002023352126@10.72.38.12:5061>\r\n"
            "Call-ID: 3103485424200000000200841135310372313125506042000000002008411353103723131255060\r\n"
            "CSeq: 5287302 MESSAGE\r\n"
            "Contact: <sip:42000000002008411353@10.72.1.125:5060>\r\n"
            "User-Agent: SIP .NET 1.0 evaluation version\r\n"
            "Max-Forwards: 70\r\n"
            "Content-Type: application/global_eye_v10+xml\r\n"
            "Content-Length: 434\r\n"
            "\r\n"
            "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Message Version=\"1.0\">\r\n"
            "<IE_HEADER MessageType=\"MSGX_STOP_MEDIA_REQ\" SequenceNumber=\"1\" SessionID=\"\" SourceID=\"42000000002008411353\" DestinationID=\"42000000002023352126\" />\r\n"
            "<IEX_MEDIA_LINK MediaSessionId=\"1_42010606001320082255_1_1_1\" MediaDirection=\"1\" MediaSrcPort=\"0\" RemoteIp=\"10.73.0.213\" RemotePort=\"20612\" LocalIp=\"10.72.38.12\" LocalPort=\"27428\" TransType=\"0\" ConnMode=\"0\"/>\r\n"
            "</Message>\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "MESSAGE");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_MESSAGE);
    CU_ASSERT_EQUAL(i_content_len, 434);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 正确的请求头
 * UPDATE
 */
static void __case14(void)
{
    char *p_payload = (char *) "UPDATE sip:+8675552371020@s15.huawei.com SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 191.138.100.8:5060;branch=z9hG4bKm8ln79nnz91w91kjl09yxj779;Role=3;Dpt=7884_16;TRC=34c5-ffffffff,SIP/2.0/UDP 191.138.100.8:5060;branch=z9hG4bKzx99y991zlz7lkz1kyiw8jjyn;Role=3;Dpt=7884_16;TRC=34c5-ffffffff,SIP/2.0/UDP 191.138.100.10:5061;branch=z9hG4bKufg544r4w5g1vrurxgi5fiqvq\r\n"
            "Route: <sip:191.138.100.10:5060;lr>,<sip:191.138.100.8;lr;ORGDLGID=6511-3;Dpt=7884_6;TRC=34c5-ffffffff>\r\n"
            "Call-ID: hsfou1wwwuiih15r4vuwv4fioxs1vgrq@ATS.ats.s15.huawei.com.22\r\n"
            "From: <sip:+8675552371010@s15.huawei.com;user=phone>;tag=s4yv4hyo-CC-22-TRC-5650\r\n"
            "To: <sip:+8675552371020@s15.huawei.com;user=phone>\r\n"
            "CSeq: 1 UPDATE\r\n"
            "Contact: <sip:+8675552371010@s15.huawei.com;user=phone>\r\n"
            "Max-Forwards: 69\r\n"
            "P-Charging-Vector: icid-value=\"a096f52912767f6b1d56de9ec4708248.3517286331.242497.53\";orig-ioi=scscf.home1.com;term-ioi=scscf.home1.com\r\n"
            "Session-Expires: 600\r\n"
            "Supported: timer\r\n"
            "Min-SE: 600\r\n"
            "Content-Length: 202\r\n"
            "Content-Type: application/sdp\r\n"
            "\r\n"
            "v=0\r\n"
            "o=HuaweiATS9900 1360 1361 IN IP4 191.138.100.10\r\n"
            "s=Sip Call\r\n"
            "c=IN IP4 191.138.100.8\r\n"
            "t=0 0\r\n"
            "m=audio 10788 RTP/AVP 0 8 121 101\r\n"
            "a=rtpmap:121 AMR/8000\r\n"
            "a=rtpmap:101 telephone-event/8000\r\n"
            "a=sendrecv\r\n\r\n";

    int i_ret;
    int i_is_req = FR_NO;
    L7_sip_packet *p_st_sip_packet = NULL;
    if ((p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet))) == NULL)
        return;
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));
    if (sip_init_packet(p_st_sip_packet) != FR_SUCC)
        return;
    int i_content_len = 0;

    i_ret = sip_analyze_header(p_payload, strlen(p_payload), &i_is_req, &(p_st_sip_packet->p_req_method), &(p_st_sip_packet->em_req_method), &i_content_len);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(i_is_req, FR_YES);
    CU_ASSERT_STRING_EQUAL(p_st_sip_packet->p_req_method, "UPDATE");
    CU_ASSERT_EQUAL(p_st_sip_packet->em_req_method, SIP_METHOD_TYPE_UPDATE);
    CU_ASSERT_EQUAL(i_content_len, 202);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

/**
 * 错误的请求头
 */
static void __case15(void)
{
    char *p_payload = (char *) "INVIT sip:42010606001320082255_1@10.72.1.125 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "a=recvonly\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n\r\n";

    int i_ret = sip_analyze_header(p_payload, strlen(p_payload), NULL, NULL, NULL, NULL);
    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
}

/**
 * 错误的请求头
 */
static void __case16(void)
{
    char *p_payload = (char *) "INVITE sip:42010606001320082255_1@10.72.1.125 SIP/2.\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "a=recvonly\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n\r\n";

    int i_ret = sip_analyze_header(p_payload, strlen(p_payload), NULL, NULL, NULL, NULL);
    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
}

/**
 * 错误的请求头
 */
static void __case17(void)
{
    char *p_payload = (char *) "INVITE sip:42010606001320082255_1@10.72.1.125 SI/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "a=recvonly\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n\r\n";

    int i_ret = sip_analyze_header(p_payload, strlen(p_payload), NULL, NULL, NULL, NULL);
    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
}

int add_suite_sip_analyze_header(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" sip_analyze_header()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL ||
        CU_add_test(pSuite, "case8", __case8) == NULL ||
        CU_add_test(pSuite, "case9", __case9) == NULL ||
        CU_add_test(pSuite, "case10", __case10) == NULL ||
        CU_add_test(pSuite, "case11", __case11) == NULL ||
        CU_add_test(pSuite, "case12", __case12) == NULL ||
        CU_add_test(pSuite, "case13", __case13) == NULL ||
        CU_add_test(pSuite, "case14", __case14) == NULL ||
        CU_add_test(pSuite, "case15", __case15) == NULL ||
        CU_add_test(pSuite, "case16", __case16) == NULL ||
        CU_add_test(pSuite, "case17", __case17) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
